

package com.hazelcast.it;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

@RunWith(HazelcastParallelClassRunner.class)
public class CheckNoticeFileIT {

    @Test
    @Category(QuickTest.class)
    public void verifyNoticeFile() throws IOException {
        assertResourcePresence(true, "META-INF/NOTICE");
        assertResourcePresence(false, "META-INF/NOTICE.txt");
    }

    private void assertResourcePresence(boolean isResourceExpected, String resourcePath) throws IOException {
        ClassLoader cl = getClassFromPackage().getClassLoader();
        Enumeration<URL> resources = cl.getResources(resourcePath);
        while (resources.hasMoreElements()) {
            String urlString = resources.nextElement().toString();
            if (urlString.contains("target")) {
                if (isResourceExpected) {
                    return;
                } else {
                    Assert.fail("Resource " + resourcePath + " was not expected, but found in " + urlString);
                }
            }
        }
        if (isResourceExpected) {
            Assert.fail("Resource " + resourcePath + " was expected, but was not found.");
        }
    }

    protected Class<?> getClassFromPackage() {
        return HazelcastInstance.class;
    }
}
